package leetcode.数组;

import java.util.ArrayList;
import java.util.List;

public class 汇总区间228 {
    public static List<String> summaryRanges(int[] nums) {
        List<String> ans = new ArrayList<>();
        if(nums == null || nums.length == 0)
            return ans;
        int start = nums[0];
        int end = nums[0];
        for (int i = 0;i<nums.length;i++) {
            int num = nums[i];
            if(num != end){
                if(start != nums[i-1])
                    ans.add(start + "->" + nums[i-1]);
                else
                    ans.add(String.valueOf(start));
                start = num;
                end = num;
            }
            end++;

        }
        if(nums[nums.length-1] != start)
            ans.add(start + "->" +  nums[nums.length-1]);
        else ans.add(String.valueOf(end-1));
        return ans;
    }

    public static void main(String[] args) {
        int[] nums = {0,1,2,4,5};
        summaryRanges(nums);
    }
}
